package com.uniknow.app.action;

import java.sql.SQLException;
import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.uniknow.app.AppConfig;
import com.uniknow.app.bin.TBCallLog;
import com.uniknow.app.bin.TBSMS;
import com.uniknow.app.manager.DatabaseHelper;
import com.uniknow.app.manager.SMSManager;
import com.uniknow.app.operate.BaseListener;
import com.uniknow.app.util.StringUtils;

/**
 * SMS表Action
 * @author lianjiao 
 * @created 2013-11-21
 */
public class ActionSMS {
	private Context mContext;
	private SMSManager smsManager;
	
	public ActionSMS (Context context){
		super();
		mContext = context;
		smsManager = new SMSManager(mContext, TBSMS.class);
	}
	/**
	 * 初始化SMS表数据
	 * @author lianjiao 
	 * @created 2013-11-21
	 */
	public void init(final BaseListener listener){
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				try {
					initSms(mContext);
					listener.onSuccess("");
				} catch (SQLException e) {
					e.printStackTrace();
					listener.onError(e.getMessage());
				}
			}
		}).start();

	}
	/**
	 * 方法名：initSms
	 * 描述：初始化短信表数据，先清空表，然后从系统中获取短信重新插入表中
	 * @参数：   参数名  参数类型   参数描述
	 * @返回值类型： void   
	 * @创建时间：  2015年3月13日 
	 * @创建者：韩创
	 * @变更记录：2015年3月13日下午4:01:42 by
	 */
	public static void initSms(Context mContext) throws SQLException{
		SMSManager smsManager = new SMSManager(mContext, TBSMS.class);
		DatabaseHelper.getHelper(mContext).deleteData(com.uniknow.app.bin.TBSMS.class);
		ArrayList<TBSMS> list = smsManager.GetSMSList(0);
		smsManager.InsertSMS(list);
	}
	
	public static void appendSms(Context mContext){
		SMSManager smsManager = new SMSManager(mContext, TBSMS.class);
		TBSMS lastSms = smsManager.getFirst(false);
		if(lastSms!=null && StringUtils.isAllNumber(lastSms.getDate())){
			int lastTime = Integer.parseInt(lastSms.getDate());
			ArrayList<TBSMS> list = smsManager.GetSMSList(lastTime);
			smsManager.InsertSMS(list);
		}
	}
	/**
	 * 根据tel和时间段，统计短信数量
	 * @author lianjiao 
	 * @created 2013-11-27
	 * @param tel
	 * @param time
	 * @return
	 */
	public int GetSMSCount(String tel,int time){
		int count = 0;
		//DatabaseHelper dbHelper = DatabaseHelper.getInstance(mContext);
		SQLiteDatabase db = DatabaseHelper.getHelper(mContext).getWritableDatabase();//DatabaseHelper.OpenDataBase(mContext);
		String sql = "select * from "+AppConfig.TB_PHONE_SMS+" where date>(strftime('%s',date('now')))-2592000*"+(StringUtils.isEmpty(time+"")?12:time);
		if(!StringUtils.isEmpty(tel)){
			sql+=" and tel = "+tel;
		}
		System.out.println("sql = "+sql);
		Cursor cursor = db.rawQuery(sql, null);
		count = cursor.getCount();
		cursor.close();
		return count;
	}
	/**
	 * 根据参数获得短信联系频率列表
	 * @author lianjiao 
	 * @created 2013-12-17
	 * @param isdesc
	 * @param is11
	 * @param months
	 * @return
	 */
	public ArrayList<TBSMS> GetSMSListByParam(boolean isdesc,boolean is11,int months){
		ArrayList<TBSMS> listSMS = new ArrayList<TBSMS>();
		TBSMS tbSMS;
		SQLiteDatabase db = DatabaseHelper.getHelper(mContext).getWritableDatabase();//DatabaseHelper.OpenDataBase(mContext);
		String sql = "select _id,type,date,tel,size,count(tel) as times from "+AppConfig.TB_PHONE_SMS+" where 1=1";
		   	   sql+=" and date>(strftime('%s',date('now')))-2592000*"+(StringUtils.isEmpty(months+"")?12:months);
		   	   if(is11) sql+=" and length(tel)=11";
		   	   if(isdesc) sql+=" group by tel order by times desc"; else sql+=" group by tel order by times asc";
		Cursor cursor = db.rawQuery(sql, null);
		if (cursor != null) {
			while (cursor.moveToNext()) {
				tbSMS = new TBSMS();
				tbSMS.set_id(cursor.getInt(0));
				tbSMS.setType(cursor.getInt(1));
				tbSMS.setDate(cursor.getString(2));
				tbSMS.setTel(cursor.getString(3));
				tbSMS.setSize(cursor.getInt(4));
				tbSMS.setTimes(cursor.getInt(5));
				listSMS.add(tbSMS);
			}
		}
		cursor.close();
		//db.close();
		//dbHelper.close();
		return listSMS;
		
	}

}
